package com.movitech.mobile.cz.base.controller;

import com.google.common.collect.Lists;
import com.movitech.mobile.cz.base.common.util.excel.ExportExcel;
import com.movitech.mobile.cz.base.entity.*;
import com.movitech.mobile.cz.base.impl.SupplierReportService;
import com.movitech.mobile.cz.base.modules.controller.RestReturnResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Created by robie.cao on 2018/5/18.
 */
@RestController
@RequestMapping("/v1/supplierReport")
@Api(value = "供方模块报表", description = "供方模块报表")
public class SupplierReportController {

    @Autowired
    private SupplierReportService supplierReportService;

    @PostMapping("/qualifiedSupplier")
    @ApiOperation(value = "新增合格供应商数量统计", notes = "新增合格供应商数量统计")
    public RestReturnResult qualifiedSupplier(@RequestBody QualifedSupplierReqVO qualifedSupplierReqVO) {
        RestReturnResult restReturnResult = RestReturnResult.success();
        restReturnResult.setData(supplierReportService.qualifiedSupplierCount(qualifedSupplierReqVO));
        return restReturnResult;
    }

    @PostMapping("/exportQualifiedSupplier")
    @ApiOperation(value = "新增合格供应商数量统计导出", notes = "新增合格供应商数量统计导出")
    public void exportQualifiedSupplier(@RequestBody QualifedSupplierReqVO qualifedSupplierReqVO,
                                                    HttpServletResponse response, HttpServletRequest request) {
        try {
            String fileName = "新增合格供应商数量统计.xlsx";
            new ExportExcel("新增合格供应商数量统计", QualifedSupplierRspVO.class).setDataList(supplierReportService.qualifiedSupplierCount(qualifedSupplierReqVO))
                    .write(request,response, fileName).dispose();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @PostMapping("/qualifiedSupplierDtl")
    @ApiOperation(value = "新增合格供应商详情", notes = "新增合格供应商详情")
    public RestReturnResult qualifiedSupplierDtl(@RequestBody QualifedSupplierReqVO qualifedSupplierReqVO) {
        RestReturnResult restReturnResult = RestReturnResult.success();
        restReturnResult.setData(supplierReportService.qualifiedSupplierDtl(qualifedSupplierReqVO));
        return restReturnResult;
    }

    @PostMapping("/exportQualifiedSupplierDtl")
    @ApiOperation(value = "新增合格供应商详情统计导出", notes = "新增合格供应商详情统计导出")
    public void exportQualifiedSupplierDtl( @RequestBody QualifedSupplierReqVO qualifedSupplierReqVO,
                                            HttpServletResponse response,HttpServletRequest request) {
        try {
            String fileName = "新增合格供应商详情.xlsx";
            new ExportExcel("新增合格供应商详情", ExtSSupplierRcRelateRsp.class).setDataList(supplierReportService.qualifiedSupplierDtl(qualifedSupplierReqVO))
                    .write(request,response, fileName).dispose();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @PostMapping("/exportlevelCount")
    @ApiOperation(value = "供应商星级统计", notes = "供应商星级统计")
    public void exportlevelCount(@RequestBody StarLevelCountReqVO starLevelCountReqVO,
                                 HttpServletResponse response,HttpServletRequest request) {
        try {
            List<List<StarLevelCountRspVO>> starLevels = supplierReportService.levelCountNew(starLevelCountReqVO);
            List<StarLevelCountRspVO> exportDate = Lists.newArrayList();
            for(List<StarLevelCountRspVO> levelCountRspVOS :starLevels){
                exportDate.addAll(levelCountRspVOS);
            }
            String fileName = "供应商星级统计详情.xlsx";
            new ExportExcel("供应商星级统计详情", StarLevelCountRspVO.class).setDataList(exportDate)
                    .write(request,response, fileName).dispose();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @PostMapping("/levelCount")
    @ApiOperation(value = "供应商星级统计", notes = "供应商星级统计")
    public RestReturnResult levelCount(@RequestBody StarLevelCountReqVO starLevelCountReqVO) {
        RestReturnResult restReturnResult = RestReturnResult.success();
        restReturnResult.setData(supplierReportService.levelCountNew(starLevelCountReqVO));
        return restReturnResult;
    }


    @GetMapping("/getFixedClassify")
    @ApiOperation(value = "获取统计分类项", notes = "获取统计分类项")
    public RestReturnResult getFixedClassify() {
        RestReturnResult restReturnResult = RestReturnResult.success();
        restReturnResult.setData(supplierReportService.getFixedClassifys());
        return restReturnResult;
    }





}
